package com.yolo.controller;



import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yolo.entity.Emp;
import com.yolo.entity.Errand;
import com.yolo.entity.dto.ErrandDto;
import com.yolo.entity.dto.SalaryDto;
import com.yolo.service.EmpService;
import com.yolo.service.ErrandDtoService;
import com.yolo.service.ErrandService;
import com.yolo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 霜蟾
 * @since 2021-04-26
 */
@RestController
@RequestMapping("/errand")
@CrossOrigin
public class ErrandController {

    @Autowired
    private ErrandService errandService;

    @Autowired
    private ErrandDtoService errandDtoService;

    @Autowired
    private EmpService empService;

    Result result=new Result();
    @RequestMapping("/selectAll")
    public Result selectAll() {
        List<ErrandDto> errandDtos = errandDtoService.selectAll();

        return Result.succ(errandDtos);
    }


    @DeleteMapping("/batchDelete/{errandIds}")
    public Result delete(@PathVariable("errandIds") String errandIds) {
        String[] split = errandIds.split(",");
        boolean b = errandService.removeByIds(Arrays.asList(split));
        return Result.succ(b);
    }
    @RequestMapping("/selectAllList")
    public Result selectList(int pageIndex, int pageSize, String name,String month) throws ParseException {
        if(month=="" || month==null )
        {
            month = "2021-04-1";
        }

        Page<ErrandDto> page=new Page<>(pageIndex,pageSize);
        QueryWrapper qw=new QueryWrapper();
        qw.like("name",name);
        qw.apply(" DATE_FORMAT( errand_time, '%Y%m' ) = DATE_FORMAT( '"
                        + month + "', '%Y%m' ) ",
                null);
        Page<ErrandDto>page1=errandDtoService.page(page,qw);
        return Result.succ(MapUtil.builder()
                .put("count", page1.getTotal())
                .put("data", page1.getRecords())
                .put("limits", page1.getPages()).map());

    }

    @RequestMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id)
    {
        Emp emp_id = empService.getById(id);
        return Result.succ(emp_id);
    }

    @RequestMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id)
    {
        Errand byId = errandService.getById(id);
        return Result.succ(byId);
    }

    @DeleteMapping("/delete/{errandid}")
    public Result delete(@PathVariable("errandid")Integer errandId)
    {
        boolean b = errandService.removeById(errandId);
        return Result.succ(b);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Errand errand)
    {
        boolean b = errandService.updateById(errand);
        return result.succ(b);
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody Errand errand)
    {
        boolean save = errandService.save(errand);
        Result rs=result.succ(save);
        System.out.println(rs);
        return rs;
    }
}
